<template>
  <div>
    <div class="printOrder">
      <el-row>
        <el-col :span="24">
          <label class="durg_title">盘点详情</label>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <table class="table-box">
            <thead>
              <tr>
                <td colspan="5">盘点日期：{{ detail.gmtCreate }}</td>
                <td colspan="7">盘点人员：{{ detail.memberName }}</td>
              </tr>
              <tr>
                <th>商品名称</th>
                <th>规格</th>
                <th>单位</th>
                <th>生产厂商</th>
                <th>供应商</th>
                <th>批号</th>
                <th>成本价</th>
                <th>有效期</th>
                <th>账面数量</th>
                <th>实际数量</th>
                <th>盘盈盘亏</th>
                <th>描述</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item, index) in tableList" :key="index">
                <td>{{ item.goodsName }}</td>
                <td>{{ item.specs }}</td>
                <td>{{ item.totalQuantityUnit | changeAssist(dwList) }}</td>
                <td>{{ item.factory }}</td>
                <td>{{ item.supplierName }}</td>
                <td>{{ item.lotNumber }}</td>
                <td>{{item.costMoney}}</td>
                <td>{{ item.effectiveTime }}</td>
                <td>{{ item | residueDurg(dwList) }}</td>
                <td>
                  {{ item.actualNum
                  }}{{ item.totalQuantityUnit | changeAssist(dwList) }}
                  <label v-if="item.isPiece == 1"
                    >{{ item.actualSubNum
                    }}{{ item.actualSubNumUnit | changeAssist(dwList) }}</label
                  >
                </td>
                <td :style="{ color: item.type == 2 ? '#EC0101' : '' }">
                  <label v-if="item.type == 2">-</label>
                  {{ item.changeNum }}
                  {{ item.actualNumUnit | changeAssist(dwList) }}
                  <label v-if="item.smallChangeNum">
                    {{ item.smallChangeNum }}
                    {{ item.actualSubNumUnit | changeAssist(dwList) }}
                  </label>
                </td>
                <td>{{ item.remark }}</td>
              </tr>
            </tbody>
          </table>
        </el-col>
      </el-row>
    </div>
    <br />
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <br />
    <span slot="footer">
      <el-button v-if="detail.status == 2" type="primary" @click="onExamine"
        >审核</el-button
      >
      <el-button @click="onClose">返回</el-button>
      <el-button @click="onDerive" style="float:right;">
        <i class="iconfont icondayin"></i>导出
      </el-button>
    </span>
  </div>
</template>
<script>
import Valid from "@/_js/validate.js";
import Excel from "@/_js/excel.js";
import util from "@/_js/utils.js";
export default {
  data() {
    return {
      btnLoading: false,
      detail: "",
      pager: { pageSize: 10, pageNum: 1, total: 0, source: 1 },
      tableList: [],
      wayList: [],
      dwList: [],
      wdwList: [],
    };
  },
  mounted() {
    let th = this;
    th.verifyUrlAttack(th.$route.query).then(() => {
      th.durg_id = th.jieMi(th.$route.query.id);
      th.onOpen();
      th.getData();
      th.getBasicData();
    });
  },
  methods: {
    onOpen() {
      let th = this;
      th.$api.findMedicalInventoryById({ id: th.durg_id }).then((res) => {
        if (res.executed) {
          th.detail = res.data.medicalInventoryInfo;
        }
      });
    },
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      let j = { id: th.durg_id, source: 1 };
      Object.assign(j, th.pager);
      th.showLoading();
      th.$api.findMedicalInventoryItemPageV2(j).then((res) => {
        if (res.executed) {
          if (res.dataBody != undefined) {
            let list = res.dataBody.list;
            list.map((it) => {
              if (it.type != 3) {
                let totalActualNum =
                  it.actualNum * it.preparationNum + it.actualSubNum;
                it.changeNum = it.changeNum;
                it.smallChangeNum = Math.floor(
                  Number(totalActualNum - it.currentNum) % it.preparationNum
                );
                it.smallChangeNum = Math.abs(it.smallChangeNum);
              }
            });
            th.tableList = list;
            th.pager.total = res.dataBody.total;
          } else {
            th.tableList = [];
            th.pager.total = 0;
          }
        }
        th.showLoading(false);
      });
    },
    onClose() {
      let th = this;
      let type = th.$route.query.type;
      th.$router.push({
        path: "/page/drugIndex",
        query: th.getUrlToken({ id: th.jiaMi(4) }),
      });
    },
    onExamine() {
      let th = this;
      th.$confirm(`确定审核通过吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(() => {
        let j = { id: th.durg_id };
        th.showLoading();
        th.$api.examinInventoryV2(j).then((res) => {
          if (res.executed) {
            th.$message.success("审核成功");
            th.onClose();
          } else {
            th.$message.error(res.message);
          }
          th.showLoading(false);
        });
      });
    },
    //打印
    doPrint() {
      let _this = this;
      window.document.body.innerHTML = document.getElementsByClassName(
        "printOrder"
      )[0].innerHTML;
      window.print();
      window.location.reload();
    },
    getBasicData() {
      let th = this;
      th.getCommonConfigByKey("药品剂量单位").then((res) => {
        th.dwList = res;
      });
      th.getCommonConfigByKey("单位").then((res) => {
        th.wdwList = res;
      });
    },
    //导出
    onDerive() {
      let out = [];
      let th = this;
      th.dwList.forEach((it) => {
        out[it.id] = it.desc;
      });
      // isChange:1
      th.$api.findMedicalInventoryItemPageV2({ source: 1, id: th.durg_id }).then((res) => {
          if (res.executed) {
            if (res.dataBody != undefined) {
              let list = res.dataBody.list;
              list.map((it) => {
                it.accountNum = util.surplusDurg(it, th.dwList);
                it.gmtCreate = th.detail.gmtCreate;
                it.memberName = th.detail.memberName;
                 if (it.isPiece==1) {
                     it.actualNumStr = it.actualNum + util.getDwByDwId(it.totalQuantityUnit, this.dwList) + it.actualSubNum +  util.getDwByDwId(it.actualSubNumUnit, this.dwList);
                 }else{
                    it.actualNumStr = it.actualNum + util.getDwByDwId(it.totalQuantityUnit, this.dwList);
                 }
               
                let totalActualNum = it.actualNum * it.preparationNum + it.actualSubNum;
                it.smallChangeNum = Math.floor( Number(totalActualNum - it.currentNum) % it.preparationNum);
                it.smallChangeNum = Math.abs(it.smallChangeNum);
                if (it.isPiece==1) {
                    it.changeNumStr =  it.changeNum + util.getDwByDwId(it.totalQuantityUnit, this.dwList) +  it.smallChangeNum + util.getDwByDwId(it.actualSubNumUnit, this.dwList);
                  it.changeNumStr = it.type === 2 ? "-" + it.changeNumStr : it.changeNumStr;
                } else {
                  it.changeNumStr = it.changeNum +  util.getDwByDwId(it.totalQuantityUnit, this.dwList);
                  it.changeNumStr = it.type === 2 ? "-" + it.changeNumStr : it.changeNumStr;
                }
                it.effectiveTime=it.effectiveTime.substring(0, 10);
              });
              setTimeout(function() {
                Excel(
                  list,
                  [
                    { key: "invoiceNumber", name: "发票号" },
                    { key: "goodsName", name: "商品名称" },
                    { key: "specs", name: "规格" },
                    {
                      key: "totalQuantityUnit",
                      name: "单位",
                      enum: out,
                      type: "enum",
                    },
                    { key: "factory", name: "生产厂商" },
                    { key: "supplierName", name: "供应商" },
                    { key: "batchId", name: "批次" },
                    { key: "costMoney", name: "成本价"},
                    { key: "effectiveTime", name: "有效期" },
                    { key: "accountNum", name: "账面数量" },
                    { key: "actualNumStr", name: "实际数量" },
                    { key: "changeNumStr", name: "盘盈盘亏" },
                    {
                      key: "gmtCreate",
                      name: "盘点日期",
                    },
                    {
                      key: "memberName",
                      name: "盘点人员",
                    },
                    { key: "remark", name: "描述" },
                  ],
                  "盘点详情"
                );
              }, 0);
            }
          }
        });
    },
  },
  components: {},
};
</script>
<style lang="scss" scoped>
.durg_title {
  display: inline-block;
  width: 100%;
  font-size: 16px;
  border-bottom: 1px solid #ddd;
  padding-bottom: 10px;
  margin-bottom: 10px;
}
.durg_form {
  .el-input,
  .el-select {
    width: 90%;
  }
  .el-form-item__label {
    padding-bottom: 0;
  }
  .one_dose_select {
    width: 30%;
    position: absolute;
    right: 28px;
  }
}
</style>
